package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class ConfirmOrderFormServlet extends HttpServlet {

    private Order order;
    private OrderService orderService;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String cardNum = request.getParameter("cardNum");
        String expiryDate = request.getParameter("expiryDate");
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String address1 = request.getParameter("address1");
        String address2 = request.getParameter("address2");
        String city = request.getParameter("city");
        String state = request.getParameter("state");
        String zip = request.getParameter("zip");
        String country = request.getParameter("country");

        HttpSession session = request.getSession();
        order = (Order) session.getAttribute("order");
        order.setCreditCard(cardNum);
        order.setExpiryDate(expiryDate);
        order.setBillToFirstName(firstName);
        order.setBillToLastName(lastName);
        order.setBillAddress1(address1);
        order.setBillAddress2(address2);
        order.setBillCity(city);
        order.setBillState(state);
        order.setBillZip(zip);
        order.setBillCountry(country);

        OrderService orderService = new OrderService();
        orderService.updateOrder(order);  //更新订单内容
        session.setAttribute("order",order);

        order = (Order)session.getAttribute("order");
        Account account = (Account)session.getAttribute("account");

        //输出数据
        response.setContentType("text/html;charest=utf-8");
        PrintWriter out = response.getWriter();
        out.println(cardNum);
        out.flush();
        out.close();

            if(account != null){
                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + " Confirm order generation";
                logService.insertLogInfo(account.getUsername(), logInfo);
            }


    }
}
